package com.crcc.common.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ListUtil {
    /**
     * 利用List方法进行分页
     * @param list 分页数据
     * @param pagesize  页面大小
     * @param currentPage   当前页面
     */
    public static<T> List<T> pageBySubList(List<T> list, int pagesize, int currentPage) {
        int totalcount = list.size();
        if(totalcount ==0){
            return new ArrayList<>();
        }
        int pagecount = 0;
        List<T> subList;
        int m = totalcount % pagesize;
        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {
            pagecount = totalcount / pagesize;
        }
        if (m == 0) {
            subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = list.subList((currentPage - 1) * pagesize, totalcount);
            } else {
                subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
            }
        }
        return subList;
    }
    public static<T> List<T> getList(List<T> list, String like) {
        ArrayList<T> results = new ArrayList<>();
        list.forEach(obj -> {
            StringBuilder sb = new StringBuilder();
            Field[] fields = obj.getClass().getDeclaredFields();
            //设置大小写不敏感  加Pattern.CASE_INSENSITIVE
            Pattern pattern = Pattern.compile(like,Pattern.CASE_INSENSITIVE);
            for (int i = 0, len = fields.length; i < len; i++) {
                try {
                    // 获取原来的访问控制权限
                    boolean accessFlag = fields[i].isAccessible();
                    // 修改访问控制权限
                    fields[i].setAccessible(true);
                    // 获取在对象f中属性fields[i]对应的对象中的变量
                    Object o;
                    try {
                        o = fields[i].get(obj);
                        //字段是null跳过
                        if (o==null) {
                            continue;
                        }
                        String s = o.toString();
                        //根据需要排除部分属性值
                        if ("truefalse".contains(s)) {
                            continue;
                        }
                        //拼接对象中的值
                        sb.append(s);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    // 恢复访问控制权限
                    fields[i].setAccessible(accessFlag);
                } catch (IllegalArgumentException ex) {
                    ex.printStackTrace();
                }
            }
            Matcher matcher = pattern.matcher(sb);
            //模糊匹配 可根据需求调整匹配规则
            if (matcher.find()) {
                results.add(obj);
            }
        });
        return results;
    }

    /**
     * 比较两个list是否相等
     * @param list1
     * @param list2
     * @return 相等true，不等false
     */
    public static boolean checkListDiffrent(List list1, List list2) {
        list1.sort(Comparator.comparing(String::hashCode));
        list2.sort(Comparator.comparing(String::hashCode));
        return list1.toString().equals(list2.toString());
    }
}
